package com.xuyl.controller;

import com.xuyl.entity.Result;
import com.xuyl.utils.LocalUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 地址数据结构整理
 *
 * @author nisemono
 * @date 2023-02-28 04:40
 */
@RestController
@RequestMapping("/local")
public class LocalController {

    @GetMapping()
    public Result getAllAddress() {
        LocalUtil lu = LocalUtil.getInstance();
        List<String> provinces = lu.getProvinces("中国");
        List<Map> allAddress = new ArrayList<>();
        for (String p : provinces) {

            Map province = new HashMap();   // 存放一个省的信息
            allAddress.add(province);   // 存放所有省

            province.put("value", p);
            province.put("label", p);
            List cs = new ArrayList();      // 存放一个省的所有城市
            province.put("children", cs);   // 存放省下的城市


            List<String> cities = lu.getCities("中国", p);
            for (String c : cities) {

                Map city = new HashMap();
                cs.add(city);               // 存放所有城市

                city.put("value", c);
                city.put("label", c);
                List cts = new ArrayList();
                List<String> counties = lu.getcounty("中国", p, c);
                if (counties != null && counties.size() > 0) {
                    city.put("children", cts);
                    for (Object ct : counties) {
                        Map county = new HashMap();
                        county.put("value", ct);
                        county.put("label", ct);
                        cts.add(county);
                    }
                }

            }

        }
        return new Result(true, "查询搜有地址成功", allAddress);
    }
}
